<template>
  <view class="friendDetails">
    <topTitle title="用户资料" />
    <view class="com">
      <view>
        <view><up-avatar class="avatar" mode="aspectFill" :src="friendInfo.picture" size="70"></up-avatar></view>
        <view>
          <view class="user_name">{{ friendInfo.user_name }}</view>
          <view class="nick_name">昵称：{{ friendInfo.nick_name }}
            <i class="sex" :class="{ female: friendInfo.sex==1 }"></i>
          </view>
        </view>
      </view>
      <view class="notes" @tap="
        notesShow = true;
      notes = friendInfo?.notes;
      ">
        <view>备注</view>
        <view v-if="friendInfo?.notes">{{ friendInfo.notes }}</view>
      </view>
    </view>
    <div class="detail">
      <view>
        <view>置顶聊天</view>
        <view><up-switch v-model="topSticky" @change="topStickyChange" activeColor="#34C759"></up-switch></view>
      </view>
      <view>
        <view>消息免打扰</view>
        <view><up-switch v-model="notDisturb" @change="notDisturbChange" activeColor="#34C759"></up-switch></view>
      </view>
      <view class="arrow" @tap="FriendDeleteFn">
        <view>删除好友</view>
      </view>
      <view class="arrow" @tap="clearChatHistory">
        <view>清空聊天记录</view>
      </view>
    </div>
    <view class="btns">
      <view @tap="goDetail">
        <i></i>发送消息
      </view>
      <view @tap="handleCall(2, friendInfo,1)"><i></i>视频通话</view>
      <view @tap="handleCall(1, friendInfo,1)"><i></i>语音通话</view>
    </view>

    <up-modal :show="notesShow" confirmText="确认" cancelText="取消" title="修改备注" :showCancelButton="true"
      @cancel="notesShow = false" @confirm="confirm">
      <up-input placeholder="请输入备注" border="surround" focus v-model="notes"></up-input>
    </up-modal>
  </view>
</template>
<script setup lang="ts">
import { ref, getCurrentInstance } from "vue";
import { onLoad } from "@dcloudio/uni-app";
import { FriendInfoSelect, FriendUpdate, FriendDelete } from "@/api/ImApi";
import { stringify } from "qs";

const instance = getCurrentInstance()?.appContext.config.globalProperties;

let friendInfo = ref<any>({});
let user_id = ref<number>();
  let chatId = ref<string>();
let topSticky = ref<boolean>(false);
let notDisturb = ref<boolean>(false);
let notesShow = ref<boolean>(false);
let notes = ref<string>();
onLoad(async (options: any) => {
  console.log(options)

  user_id.value = Number(options.chatId);
  chatId.value =String(options.chatId);
  let friendList = await instance?.$db.loadData("friendList", {
    id: user_id.value,
  });
  console.log(friendList)

  if (friendList?.length) {
    friendInfo.value = friendList[0];
  } 
  FriendInfoSelect({
    user_id: user_id.value
  }).then((e)=>{
    friendInfo.value={
      ...friendInfo.value,
      ...e
    }
  })

  let chatList = await instance?.$db.loadData("chatList", {
    id: user_id.value,
  });

  topSticky.value = chatList[0]?.topSticky == 1 ? true : false
  notDisturb.value = chatList[0]?.notDisturb == 1 ? true : false

});

const confirm = async () => {
  await FriendUpdate({ notes: notes.value, user_id: user_id.value });
  await instance?.setChatList(
    {
      notes: notes.value,
    },
    user_id.value || ""
  );
  notesShow.value = false;
  friendInfo.value.notes = notes.value;
  notes.value = "";
  uni.setStorageSync("submitSendRed", 1);
};

// 置顶
const topStickyChange = () => {
  friendInfo.value.topSticky = topSticky.value ? 1 : 0;
  if (user_id.value) {
    instance?.setChatList(friendInfo.value, user_id.value);
  }
}
// 消息免打扰
const notDisturbChange = () => {
  friendInfo.value.notDisturb = notDisturb.value ? 1 : 0;
  if (user_id.value) {
    instance?.setChatList(friendInfo.value, user_id.value);
  }
}
// 清空聊天记录
const clearChatHistory = () => {
  uni.showModal({
    title: "温馨提示",
    content: "确认清空聊天记录？",
    confirmText: "确认",
    cancelText: "取消",
    success: async (r) => {
      if (r.confirm) {
        await instance?.deletingData({
          ...friendInfo.value,
          chatId: chatId.value

        });
      }
    },
  });
}




const FriendDeleteFn = () => {
  uni.showModal({
    title: "温馨提示",
    content: "确认删除好友？",
    confirmText: "确认",
    cancelText: "取消",
    success: async (r) => {
      if (r.confirm) {
        await FriendDelete(
          {
            user_id: user_id.value,
          },
          {
            ...friendInfo.value,
            chatId: chatId.value
          }
        );
        instance?.goReLaunch("/pages/index/index", "删除成功");
      }
    },
  });
};
// 进入聊天
const goDetail = async () => {
  instance?.goTo(
    "/pages/chat/chat?user_id=" + friendInfo.value.user_id + "&Type=1"
  );
};
</script>

<style scoped lang="scss">
@import "../../static/scss/chat.scss";
</style>
